<template>
	<view class="workDetail">
		<view class="" style="position: fixed;top: 0;left: 0;width: 750rpx;z-index: 99999;" v-if="ceShow">
			<u-notice-bar mode="horizontal" :list="noticeList"></u-notice-bar>
		</view>
		<u-modal v-model="show" :show-title="false" width="690" :show-confirm-button="false" z-index="20">
			<view class="slot-content">
				<view class="dialogMain">
					<view class="dialogMain_top">
						<image src="../../static/img/close.png" mode="" @click="closeDialog"></image>
					</view>
					<view class="" style="padding: 0 18rpx;border-bottom: 1rpx solid #e2e2e2;">
						<span>{{nowText}}</span>
						<u-input v-model="value" type="textarea" :border="false" :height="300" />
					</view>
					<view class="" v-if="can_upload == '1'">
						<u-upload :header="header" :action="action" :file-list="fileList" @on-success="success"
							@on-remove="remove" :max-count="9" width="181" height="181" upload-text=""></u-upload>
					</view>
					<view class="dialogMain_bottom">
						<view class="" @click="submit">
							发布
						</view>
					</view>
				</view>
			</view>
		</u-modal>

		<view class="workDetail_header">
			<view class="workDetail_header_left" @click="goWorkerDetail" v-if="info.user">
				<image :src="info.user.avatar" mode=""></image>
				<span>{{info.user.nickname}}</span>
			</view>
			<view class="workDetail_header_right">
				{{timestamp(info.createtime)}}
			</view>
		</view>
		<view class="workDetail_body">
			<view class="workDetail_body_title">
				{{info.title}}
			</view>
			<view class="workDetail_body_content" v-html="info.content">
			</view>
			<!-- <view class="workDetail_body_img_item">
				<image :src="info.image" mode="" @click="handleImage([info.image],0)"></image>
			</view> -->
			<view class="workDetail_body_video_item" v-if="info.file!=''">
				<video :src="info.file" controls style="width: 100%;height: 100%;"></video>
			</view>
			<image class="workDetail_body_img_item" :src="item" mode="widthFix" v-for="(item,index) in info.images" :key="index" @click="handleImage(info.images,index)"></image>
		</view>
		<view class="workDetail_lyList">
			<view class="workDetail_lyList_header">
				留言
			</view>
			<view class="workDetail_lyListItem" v-for="(item,index) in list" :key="index">
				<view class="workDetail_lyListItem_header">
					<view class="workDetail_lyListItem_header_left">
						<image :src="item.user.avatar" mode=""></image>
						<span>{{item.user.nickname}}</span>
						<text>{{timestamp(item.createtime)}}</text>
					</view>
					<view class="workDetail_lyListItem_header_right" @click="reply(item)">
						回复
					</view>
				</view>
				<view class="workDetail_lyListItem_body">
					{{item.content}}
				</view>
				<view class="workDetail_lyListItem_body_imgBox" v-if="item.images && item.images.length>0">
					<view class="" v-for="(imgItem,imgIndex) in item.images" :key="imgIndex"
						style="margin-right: 10rpx;margin-bottom: 10rpx;">
						<u-image width="150rpx" height="150rpx" :src="imgItem"
							@click="handleImage(item.images,imgIndex)"></u-image>
					</view>
				</view>
			</view>
		</view>
		<view class="" style="padding: 20rpx;">
			<u-loadmore :status="status" />
		</view>
		<view class="workDetail_footer">
			<image src="../../static/img/btn1.png" mode="" @click="openDialog"></image>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex';
	import {
		baseUrl
	} from '@/config/config.js'
	import {
		getWorkDetail,
		getWorkDetailLy,
		getConfig
	} from '@/api/index.js'
	export default {
		data() {
			return {
				action: '',
				id: '',
				info: {},
				page: 1,
				limit: 10,
				list: [],
				status: 'loadmore',
				show: false,
				nowText: '',
				value: '',
				fileList: [],
				baseUrl: '',
				header: {
					token: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').token : ''
				},
				replyUid: '',
				can_upload: '1',
				ceShow: false,
				noticeList: []
			}
		},
		watch: {
			socketMessage(newVal, oldVal) {
				if (newVal) {
					this.ceShow = true
					this.noticeList = [newVal.slice(0, -1)]
					setTimeout(() => {
						this.ceShow = false
					}, 10000)
				}
			}
		},
		computed: {
			...mapGetters(['getSocketMessage']),
			socketMessage() {
				return this.getSocketMessage;
			}
		},
		methods: {
			goWorkerDetail() {
				uni.navigateTo({
					url: `/pages/views/workerDetail?id=${this.info.user.id}`
				})
			},
			handleImage(arr, index) {
				uni.previewImage({
					current: index,
					urls: arr,
				});
			},
			async config() {
				const res = await getConfig()
				this.can_upload = res.data.can_upload
			},
			async submit() {
				// console.log(this.fileList);
				if (this.value == '') {
					return uni.showToast({
						icon: 'none',
						title: '请输入内容'
					})
				}
				let data = {
					id: this.id,
					content: this.nowText + ' ' + this.value,
					uid: this.replyUid,
					images: this.fileList.map(e => {
						return e.response.data.url
					}).join(',')
				}
				const res = await getWorkDetailLy(data)
				uni.showToast({
					icon: 'none',
					title: res.msg
				})
				this.closeDialog()
				this.page = 1
				this.list = []
				this.getInfo()
			},
			success(data, index, lists, name) {
				this.fileList = lists
			},
			remove(index, lists, name) {
				this.fileList = lists
			},
			reply(item) {
				this.nowText = `@${item.user.nickname}`
				this.replyUid = item.user_id
				this.show = true
			},
			openDialog() {
				this.nowText = ''
				this.replyUid = ''
				this.show = true
			},
			timestamp(timestamp) {
				var date = new Date(timestamp * 1000); // 转换为毫秒
				var year = date.getFullYear();
				var month = ('0' + (date.getMonth() + 1)).slice(-2);
				var day = ('0' + date.getDate()).slice(-2);
				var hours = ('0' + date.getHours()).slice(-2);
				var minutes = ('0' + date.getMinutes()).slice(-2);
				var seconds = ('0' + date.getSeconds()).slice(-2);

				return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
			},
			closeDialog() {
				this.show = false
				this.value = ''
				this.nowText = ''
				this.fileList = []
			},
			async getInfo() {
				this.status = 'loading'
				let data = {
					id: this.id,
					page: this.page,
					limit: this.limit
				}
				const res = await getWorkDetail(data)
				// console.log(res);
				this.info = res.data.info
				this.list = [...this.list, ...res.data.list.data]
				if (res.data.list.data.length < this.limit) {
					this.status = 'nomore'
				} else {
					this.status = 'loadmore'
				}
			}
		},
		onLoad(e) {
			this.ws.pageFun(this.pageDataLoad, this);
			this.baseUrl = baseUrl
			this.action = baseUrl + '/api/common/upload'
			this.id = e.id
			this.getInfo()
			this.config()
		},
		onReachBottom() {
			if (this.status == 'nomore') return
			this.page++
			this.getInfo()
		}
	}
</script>

<style scoped lang="scss">
	.workDetail {
		min-height: 100vh;
		background-color: #fff;
		padding: 30rpx;
		padding-bottom: 150rpx;

		.dialogMain {
			padding: 30rpx;

			.dialogMain_top {
				display: flex;
				justify-content: flex-end;

				image {
					width: 23rpx;
					height: 23rpx;
				}
			}

			.dialogMain_bottom {
				display: flex;
				justify-content: flex-end;
				color: #045B9B;
				padding-right: 18rpx;
				padding-bottom: 16rpx;
				margin-top: 16rpx;
			}
		}

		.workDetail_footer {
			height: 120rpx;
			background-color: #fff;
			position: fixed;
			bottom: 0;
			width: 750rpx;
			left: 0;
			z-index: 99;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 300rpx;
				height: 60rpx;
				cursor: pointer;
			}
		}

		.workDetail_header {
			padding: 20rpx 0;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.workDetail_header_left {
				display: flex;
				align-items: center;

				image {
					width: 48rpx;
					height: 48rpx;
					border-radius: 50%;
					margin-right: 20rpx;
				}
			}

			.workDetail_header_right {
				color: #999;
				font-size: 22rpx;
			}
		}

		.workDetail_body {
			.workDetail_body_title {
				font-size: 30rpx;
				font-weight: 600;
			}

			.workDetail_body_content {
				padding: 20rpx 0;
			}

			.workDetail_body_video_item {
				width: 690rpx;
				// height: 340rpx;
			}

			.workDetail_body_img_item {
				width: 690rpx;

				margin-bottom: 20rpx;
			}
		}

		.workDetail_lyList {
			.workDetail_lyList_header {
				padding: 10rpx 0;
				color: #045B9B;
				font-size: 24rpx;
				border-bottom: 1rpx solid #e2e2e2;
			}

			.workDetail_lyListItem {
				padding-bottom: 33rpx;
				border-bottom: 1rpx solid #f2f3f6;

				.workDetail_lyListItem_header {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 33rpx 0 20rpx 0;

					.workDetail_lyListItem_header_left {
						display: flex;
						align-items: center;

						image {
							width: 48rpx;
							height: 48rpx;
							margin-right: 20rpx;
							border-radius: 50%;
						}

						span {
							font-size: 24rpx;
							color: #045B9B;
							margin-right: 20rpx;
						}

						text {
							font-size: 22rpx;
							color: #999;
						}
					}

					.workDetail_lyListItem_header_right {
						font-size: 22rpx;
						color: #999;
					}
				}

				.workDetail_lyListItem_body {
					// padding: 10rpx 0;
					padding-left: 68rpx;
					font-size: 24rpx;
				}

				.workDetail_lyListItem_body_imgBox {
					padding-left: 74rpx;
					width: 554rpx;
					display: flex;
					align-items: center;
					flex-wrap: wrap;
					margin-top: 20rpx;

					image {
						width: 150rpx;
						height: 150rpx;
					}
				}
			}
		}
	}
</style>